<?php

namespace App\Http\Controllers\Api;


use App\Models\Chapter;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Article as ArticleCollection;

class ArticlesController extends ApiController
{

    /**
     *
     * @api {get} /chapters/:chapter/article 获取文章
     * @apiName Article get article
     * @apiGroup Article
     *
     * @apiParam {String} chapter 章节的id
     *
     * @apiVersion 1.0.0
     * @apiSampleRequest /api/chapters/:chapter/article
     *
     */
    public function show(Chapter $chapter){

        $article = $chapter->article;
        return new ArticleCollection($article);

    }

    /**
     *
     * @api {put} /chapters/:chapter/article 更新文章
     * @apiName Article update article
     * @apiGroup Article
     *
     * @apiParam {String} chapter 章节的id
     * @apiParam {String} article 文章内容
     *
     * @apiVersion 1.0.0
     * @apiSampleRequest /api/chapters/:chapter/article
     *
     */
    public function update(Request $request,Chapter $chapter){

        $this->validate($request,[
            'article' => 'required'
        ]);

        $article = $chapter->article()->firstOrCreate([]);

        $this->authorize('update',$article);

        $article->update([
            'article' => $request->article
        ]);
        return $this->message('更新成功');

    }


}
